#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace CreamX.Framework.Game.Components.GameComponents
{
    /// <summary>
    /// Frames per second counter. 
    /// </summary>
    /// <remarks>This class can be added to display the FPS of the current game scene.
    /// This is currently on by default and no way of removing. This should be configurable via code and xml</remarks>
    public class FrameRateCounterComponent : DrawableGameComponent
    {
        #region Private Variables
        private ContentManager _content;
        private SpriteBatch _spriteBatch;

        private int _frameRate = 0;
        private int _frameCounter = 0;
        private TimeSpan _elapsedTime = TimeSpan.Zero;
        private string _lastFPSString;
        #endregion

        public FrameRateCounterComponent(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            _content = new ContentManager(game.Services);
            Instance = this;
        }

        #region Public Properties
        public static FrameRateCounterComponent Instance;

        public string LastFrameRateString
        {
            get { return _lastFPSString; }
            set { _lastFPSString = value; }
        }

        public int LastFrameRate
        {
            get { return _frameRate; }
        }
        #endregion

        #region Public Methods
        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);
            base.LoadContent();
        }

        protected override void UnloadContent()
        {
            _content.Unload();
            base.UnloadContent();
        }

        public override void Update(GameTime gameTime)
        {
            _elapsedTime += gameTime.ElapsedGameTime;
            if (_elapsedTime > TimeSpan.FromSeconds(1))
            {
                _elapsedTime -= TimeSpan.FromSeconds(1);
                _frameRate = _frameCounter;
                LastFrameRateString = _frameRate.ToString();
                _frameCounter = 0;
            }
        }

        public override void Draw(GameTime gameTime)
        {
            _frameCounter++;

            //string __fps = string.Format("fps: {0}", _frameRate);
            //_spriteBatch.Begin(SpriteBlendMode.AlphaBlend,SpriteSortMode.Deferred,SaveStateMode.None);
            //_spriteBatch.DrawString(Text.TextEngine.Instance.GetFont((int)Text.FontNames.ArialSmall), __fps, new Vector2(33, 33), Color.Black);
            //_spriteBatch.DrawString(Text.TextEngine.Instance.GetFont((int)Text.FontNames.ArialSmall), __fps, new Vector2(32, 32), Color.White);
            
            //_spriteBatch.End();
        }
        #endregion
    }
}


